<?php

if (! defined ( 'IN_KEKE' )) {
	exit ( 'Access Denied' );
}

include_once 'Paypal.php';

// Create an instance of the paypal library


$myPaypal = new Paypal ( );

// Log the IPN results
$myPaypal->ipnLog = TRUE;

// Enable test mode if needed
$myPaypal->enableTestMode ();

// Check validity and write down it
if ($myPaypal->validateIpn ()) {
	@list($_, $uid,$order_type,$obj_id,$order_id,$model_id) = explode('-', $myPaypal->ipnData['custom'], 6);
	$total_fee = $myPaypal->ipnData['payment_gross'];
	if ($myPaypal->ipnData ['payment_status'] == 'Completed') {
		$order_obj = new Keke_witkey_pay_class ( );
		$order_obj->setWhere ( " order_id=$order_id " );
		$order_info = $order_obj->query_keke_witkey_pay ();
		if ($order_info [0] ['order_status'] == 2) {
			$order_obj->setOrder_status ( 1 );
			$order_obj->setOrder_id ( $order_id );
			$res = $order_obj->edit_keke_witkey_pay ();
			if ($res) {
				$fac_obj = new Pay_return_fac_class ( $model_id, $uid, $obj_id, $order_type, $order_id, $total_fee, 1 );
				$fac_obj->load ();
				//$order_obj = new Order_class($uid,$obj_id,$order_type,$order_id,$total_fee,1);
			}
		}
		file_put_contents ( 'paypal.txt', 'SUCCESS' . $myPaypal->ipnData );
	} else {
		file_put_contents ( 'paypal.txt', "FAILURE\n\n" . $myPaypal->ipnData );
	}
}

